package edu.uw.tcss360.group6.gui.Actions;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.*;

import edu.uw.tcss360.group6.gui.GUI;
import edu.uw.tcss360.group6.model.*;


@SuppressWarnings("serial")
public class WeavingDraftSubmissionAction extends AbstractAction{
	public WeavingDraftSubmissionAction()
	{
		super("Submit Draft");
	}
	@SuppressWarnings("static-access")
	@Override
	public void actionPerformed(ActionEvent e) {
		if(GUI.instance().getFlag() == true)
			GUI.instance().my_main_panel.setCard("edu.uw.tcss360.group6.model.Entry");
		else
			GUI.instance().my_main_panel.setCard(GUI.instance().currentUser.getClass().getSimpleName().toLowerCase() + "-splash");

        //I added this to prevent the bug where the user is not a contestant -David
        if(GUI.instance().currentUser.getClass().equals(User.class)) {
            //upgrade the user
            GUI.instance().currentUser = Contest.instance().userToContestant(GUI.instance().currentUser);
            GUI.instance().my_side_panel.logpanel.log("Upgrading user '" + GUI.instance().currentUser + "' to contestant.");
        } else if(GUI.instance().currentUser.getClass().equals(Contestant.class)) {
            //all is well
        } else {
            //INVALID, abort
            GUI.instance().my_side_panel.logpanel.log("User '" + GUI.instance().currentUser + "' cannot be upgraded to contestant as they are already a '" + GUI.instance().currentUser.getClass().toString() +  "'.");
            JOptionPane.showMessageDialog(null,"You cannot submit because you cannot become a contestant!","Error!",JOptionPane.ERROR_MESSAGE);
            return;
        }

		Contestant temp = (Contestant) GUI.instance().currentUser;
		if(GUI.instance().getdraftFlag1())
		{
			Entry entry = temp.getEntries()[0];
			WeavingDraft weave_draft = GUI.instance().my_main_panel.getWeavingPanel().getWeavingDraft();
			temp.setEntry(new Entry(weave_draft, entry.getCategory(),entry.getDescription(), temp), 0);
		    
			Contest.instance().updateUser(GUI.instance().currentUser, temp);
			GUI.instance().setdraftFlag1(false);
		}
		if(GUI.instance().getdraftFlag2())
		{
			Entry entry = temp.getEntries()[1];
			WeavingDraft weave_draft = GUI.instance().my_main_panel.getWeavingPanel().getWeavingDraft();
			temp.setEntry(new Entry(weave_draft, entry.getCategory(),entry.getDescription(), temp), 1);
			Contest.instance().updateUser(GUI.instance().currentUser, temp);
			GUI.instance().setdraftFlag2(false);
		}
		if(GUI.instance().getdraftFlag3())
		{
			Entry entry = temp.getEntries()[2];
			WeavingDraft weave_draft = GUI.instance().my_main_panel.getWeavingPanel().getWeavingDraft();
			temp.setEntry(new Entry(weave_draft, entry.getCategory(),entry.getDescription(), temp), 2);
			Contest.instance().updateUser(GUI.instance().currentUser, temp);
			GUI.instance().setdraftFlag3(false);
		}
		try {
			Contest.instance().save();
		} catch (IOException the_error) {
			the_error.printStackTrace();
			System.out.println("Couldn't save the new user");
		}
		
		
	}

}